package cn.iocoder.yudao.module.cmt.service.activity;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivitySignupDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.activity.ActivitySignupMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ActivitySignupServiceImpl implements ActivitySignupService {

    @Resource
    private ActivitySignupMapper activitySignupMapper;

    @Override
    public Long createActivitySignup(ActivitySignupSaveReqVO createReqVO) {
        ActivitySignupDO activitySignup = BeanUtils.toBean(createReqVO, ActivitySignupDO.class);
        activitySignupMapper.insert(activitySignup);
        return activitySignup.getId();
    }

    @Override
    public void updateActivitySignup(ActivitySignupSaveReqVO updateReqVO) {
        validateActivitySignupExists(updateReqVO.getId());
        ActivitySignupDO updateObj = BeanUtils.toBean(updateReqVO, ActivitySignupDO.class);
        activitySignupMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivitySignup(Long id) {
        validateActivitySignupExists(id);
        activitySignupMapper.deleteById(id);
    }

    private void validateActivitySignupExists(Long id) {
        if (activitySignupMapper.selectById(id) == null) {
            throw exception(ACTIVITY_SIGNUP_NOT_EXISTS);
        }
    }

    @Override
    public ActivitySignupDO getActivitySignup(Long id) {
        return activitySignupMapper.selectById(id);
    }

    @Override
    public PageResult<ActivitySignupDO> getActivitySignupPage(ActivitySignupPageReqVO pageReqVO) {
        return activitySignupMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivitySignupListByIds(List<Long> ids) {
        ids.forEach(this::validateActivitySignupExists);
        activitySignupMapper.deleteBatchIds(ids);
    }

}
